go

exec dbo.p_drop_proc_if_exists 'meta_add_bool_column';

go

--Function of creating boolean column
create procedure meta_add_bool_column(
    --Table id
    @a_table_id binary(16),
    --Column id
    @a_column_id binary(16),
    --Column name
    @a_column_name varchar(max))
with execute as owner
as
begin
	set nocount on;
	declare
	--Table name
    @a_table_name varchar(26),
    --Result
    @a_result numeric(1);    
    
    exec dbo.p_check_col_not_exists @a_table_id, @a_column_id, @a_column_name, @a_result output;
    
    if @a_result = 0 return;
    
    set xact_abort on;
    begin tran;
         
    set @a_table_name = dbo.p_get_table_name(@a_table_id);
         
    --Changing the metadata
    insert into meta_bool_columns(column_id, table_id, column_name,
        order_index, customization_level)
    values (@a_column_id, @a_table_id, @a_column_name,
		dbo.p_generate_order_index(@a_table_id), 1);
         
    --Dropping table view
    exec dbo.p_drop_view_if_exists @a_table_name;
         
    exec ('alter table tbl_' 
    + @a_table_name
    + ' add  ' + @a_column_name
    + ' numeric(1)');
    
    --Creating value constraint
    exec dbo.p_create_bool_col_check @a_column_id;
         
    --Creating table view
    exec dbo.p_create_table_view @a_table_id;
    
    commit;
end;

go

exec dbo.p_drop_proc_if_exists 'p_create_bool_col_check';

go

--Function of creating bool column check
create procedure p_create_bool_col_check(
	--Column id
	@a_column_id binary(16))
as
begin
	declare
	--Column name
	@a_column_name varchar(26),
	--Table name
	@a_table_name varchar(26),
    --Column id str
    @a_column_id_str varchar(32);
    
    set @a_column_id_str = dbo.p_bin_to_str(@a_column_id);
    
    --Reading column properties
    select @a_column_name = column_name,
    @a_table_name = table_name
    from meta_table_columns
    where column_id = @a_column_id;
    
	exec ('alter table tbl_'
    + @a_table_name
    + ' add constraint b' + @a_column_id_str
    + ' check(' + @a_column_name + '=0 or ' 
    + @a_column_name + '=1)');
end;

go

exec dbo.p_drop_proc_if_exists 'p_drop_bool_col_check';

go

--Function of droping a bool column check
create procedure p_drop_bool_col_check(
	--Column id
	@a_column_id binary(16))
as
begin
	declare
	--Column id str
	@a_column_id_str varchar(32),
	--Table name
	@a_table_name varchar(32);
	
	set @a_column_id_str = dbo.p_bin_to_str(@a_column_id);
	
	--Reading column properties
	select @a_table_name = table_name
	from meta_table_columns
	where column_id = @a_column_id;
	
	set @a_column_id_str = dbo.p_bin_to_str(@a_column_id);
	
	exec ('alter table tbl_' + @a_table_name
	+ ' drop constraint b' + @a_column_id_str);
end;

go

exec dbo.p_drop_proc_if_exists 'meta_set_bool_default';

go

--Function of setting column default value
create procedure meta_set_bool_default(
    --Column id
    @a_column_id binary(16),
    --Default value
    @a_default_value numeric(1))
with execute as owner
as
begin
	set nocount on;
	declare
	--Table id
    @a_table_id binary(16),
    --Table name
    @a_table_name varchar(26),
    --Column name
    @a_column_name varchar(26),
    --Customization level
    @a_customization_level numeric(10),
    --Result
    @a_result numeric(1),
    --Old default value
    @a_old_default_value numeric(1),
    --Column id str
    @a_column_id_str varchar(32);    
    
    exec dbo.p_check_col_type @a_column_id, 'meta_bool_columns', 'bool', @a_result output;
    
    if @a_result = 0 return;
    
    set @a_column_id_str = dbo.p_bin_to_str(@a_column_id);
    
    --Reading column properties
    select @a_table_id = table_id, @a_column_name = column_name,
    @a_customization_level = customization_level,
    @a_old_default_value = default_value
    from meta_bool_columns
    where column_id = @a_column_id;
    
    set @a_table_name = dbo.p_get_table_name(@a_table_id);
    
    --If this is a system column
    if @a_customization_level = 0
    begin    
        raiserror('Changing bool system column default value is forbidden (column_id = %s) (#error_code = 20008)',
        16, 1, @a_column_id_str);
        return;
    end;
    
    --If there is no need for change
    if (@a_old_default_value = @a_default_value) or
    (@a_old_default_value is null and @a_default_value is null)
		return;
    
    set xact_abort on;
    begin tran;
    
    --If there is an old default value
    if @a_old_default_value is not null
		--Dropping the old default value
		exec ('alter table tbl_' 
        + @a_table_name
        + ' drop constraint def_' + @a_column_id_str);
    
    --Updating the metadata
    update meta_bool_columns
    set default_value = @a_default_value
    where column_id = @a_column_id;
    
    --If there is a new default value
    if @a_default_value is not null
        exec ('alter table tbl_' 
        + @a_table_name
        + ' add constraint def_' + @a_column_id_str
        + ' default ' + @a_default_value + ' for ' + @a_column_name);
    
    commit;
end;

go

exec dbo.p_drop_proc_if_exists 'p_create_bool_col_constrs';

go

--Function of creating all bool column constraints
create procedure p_create_bool_col_constrs(
	--Column id
	@a_column_id binary(16))
as
begin
	exec dbo.p_create_bool_col_check @a_column_id;
end;

go

exec dbo.p_drop_proc_if_exists 'p_drop_bool_col_constrs';

go

--Function of dropping all bool column constraints
create procedure p_drop_bool_col_constrs(
	--Column id
	@a_column_id binary(16))
as
begin
	exec dbo.p_drop_bool_col_check @a_column_id;
end;